package com.qyj.spring_boot_02.util;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;
public class EmailUtil {
    public static boolean sessionEmail(String mail,String code,String username,String MAIL_USERNAME,String PASSWORD){
        Properties props = new Properties();
        props.put("mail.smtp.host","smtp.qq.com");
        props.put("mail.smtp.port","587");
        props.put("mail.smtp.auth","true");
        props.put("mail.smtp.user", MAIL_USERNAME);
        Authenticator authenticator = new Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(MAIL_USERNAME, PASSWORD);
            }
        };
        Session session = Session.getInstance(props, authenticator);
        MimeMessage message = new MimeMessage(session);
        try {
            message.setFrom(new InternetAddress(MAIL_USERNAME));
            message.setRecipient(Message.RecipientType.TO, new InternetAddress(mail));
            message.setSubject("注册青云阁APP");
            message.setText("您好,您注册京东电脑分析APP的\n验证码为："+code+"\n" +
                    "用户名：" +username+
                    "\n请保存好不要随意给其他人,有效期为5分钟。 ");
            try{
                Transport.send(message);
            }catch (Exception e){
                return false;
            }
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }
    public static boolean getPwdEmail(String mail,String code,String username,String MAIL_USERNAME,String PASSWORD){
        Properties props = new Properties();
        props.put("mail.smtp.host","smtp.qq.com");
        props.put("mail.smtp.port","587");
        props.put("mail.smtp.auth","true");
        props.put("mail.smtp.user", MAIL_USERNAME);
        Authenticator authenticator = new Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(MAIL_USERNAME, PASSWORD);
            }
        };
        Session session = Session.getInstance(props, authenticator);
        MimeMessage message = new MimeMessage(session);
        try {
            message.setFrom(new InternetAddress(MAIL_USERNAME));
            message.setRecipient(Message.RecipientType.TO, new InternetAddress(mail));
            message.setSubject("青云阁APP");
            message.setText("您好,您要改的密码的验证码如下：\n验证码为："+code+"\n" +
                    "用户名：" +username+
                    "\n请保存好不要随意给其他人,有效期为5分钟。 ");
            try{
                Transport.send(message);
            }catch (Exception e){
                return false;
            }
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }
}
